﻿using NUnit.Framework;
using System;
using NHibernate;

namespace NMA.Infrastructure.NHibernate.Tests.Mapping
{
    using NMA.Domain.Model;
    using System.Collections.Generic;
    using NMA.Infrastructure.NHibernate.DomainObject;

    [TestFixture]
    public class NewsTest : TestBaseClass
    {
        [Test]
        public void Can_add_news_to_database()
        {
            var category = new Category()
            {
                Id = new Guid(),
                Name = "test category",
                CreatedDate = DateTime.Now,
                UpdatedDate = DateTime.Now,
                CreatedUser = "abc"
            };

            ISession session = SessionFactory.OpenSession();

            Guid catId = (Guid)session.Save(category);
            session.Flush();

            session.Evict(category);            

            var news = new News
            {
                Id = new Guid(),
                Title = "test Title",
                Content = "test content",
                IsPublish = true,
                PublishedDate = DateTime.Now,
                PublishedPerson = "",
                NumberOfView = 0,
                ShortDescription = "test short description",
                Image = new byte[] { 123, 123, 123 },
                CreatedDate = DateTime.Now,
                UpdatedDate = DateTime.Now,
                CreatedUser = "test user",
                UpdatedUser = "",
                Category = session.Get<Category>(catId),
                Poll = null
            };

            var poll = new Poll()
            {
                Id = new Guid(),
                Name = "test poll",
                VoteDate = DateTime.Now,
                CreatedDate = DateTime.Now,
                UpdatedDate = DateTime.Now,
                CreatedUser = "abc"
            };

            session = SessionFactory.OpenSession();

            Guid pollId = (Guid)session.Save(poll);
            session.Flush();

            session.Evict(poll);
            
            //news.AssignPoll(poll);
            //news.AssignCategory(category);
            //news.AssignPoll(poll);

            try
            {
                session = SessionFactory.OpenSession();                
                
                Guid newsId = (Guid)session.Save(news);
                session.Flush();             

                session.Evict(news);

                var newsDb = session.Get<News>(newsId);

                Assert.IsTrue(
                        newsDb.Id == news.Id && newsDb.Title == news.Title &&
                        newsDb.Content == news.Content && newsDb.IsPublish == news.IsPublish
                    );
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                session = SessionFactory.OpenSession();
                session.Delete(news);
                session.Delete(category);
                session.Delete(poll);
                session.Flush();
                session.Evict(news);
                session.Evict(category);
                session.Evict(poll);
            }
        }

        [Test]
        public void Can_update_news_to_database()
        {
            var category = new Category()
            {
                Id = new Guid(),
                Name = "test category",
                CreatedDate = DateTime.Now,
                UpdatedDate = DateTime.Now,
                CreatedUser = "abc"
            };

            ISession session = SessionFactory.OpenSession();

            Guid catId = (Guid)session.Save(category);
            session.Flush();

            session.Evict(category);

            var news = new News
            {
                Id = new Guid(),
                Title = "test Title",
                Content = "test content",
                IsPublish = true,
                PublishedDate = DateTime.Now,
                PublishedPerson = "",
                NumberOfView = 0,
                ShortDescription = "test short description",
                Image = new byte[] { 123, 123, 123 },
                CreatedDate = DateTime.Now,
                UpdatedDate = DateTime.Now,
                CreatedUser = "test user",
                UpdatedUser = "",
                Category = session.Get<Category>(catId),
                Poll = null
            };

            try
            {
                session = SessionFactory.OpenSession();
                Guid id = (Guid)session.Save(news);
                session.Flush();

                session.Evict(news);

                var newsDb = session.Get<News>(id);

                newsDb.Title = "test update title";
                newsDb.Content = "test update content";

                session.Update(newsDb);
                session.Flush();

                session.Evict(newsDb);

                var updateNewsDb = session.Get<News>(newsDb.Id);

                Assert.IsTrue(
                        updateNewsDb.Id == newsDb.Id && updateNewsDb.Title == newsDb.Title &&
                        updateNewsDb.Content == newsDb.Content && updateNewsDb.IsPublish == newsDb.IsPublish
                    );
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                session = SessionFactory.OpenSession();
                session.Delete(news);
                session.Delete(category);
                session.Flush();
                session.Evict(news);
                session.Evict(category);
            }

        }
    }
}
